<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="650px" @confirm="handleSubmit" @close="handleClose">
			<el-form ref="formRef" :model="formData" label-width="80px" :rules="formRules">
				<el-form-item label="名称" prop="name">
					<el-input v-model="formData.name" placeholder="请输入工具名称" clearable />
				</el-form-item>
				<el-form-item label="icon" prop="icon">
					<div class="w-80">
						<div>
							<material-picker v-model="formData.icon" :limit="1" />
						</div>
						<div class="form-tips">支持扩展名：.jpg .jpeg .png .bmp</div>
					</div>
				</el-form-item>
				<el-form-item label="类型" prop="type">
					<el-radio-group v-model="formData.type">
						<el-radio :label="1">小程序</el-radio>
						<el-radio :label="2">超链接</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="链接" prop="link">
					<el-input v-model="formData.link" placeholder="请输入" clearable />
				</el-form-item>
				<el-form-item v-if="formData.type==1" label="appid" prop="app_id">
					<el-input v-model="formData.app_id" placeholder="请输入" clearable />
				</el-form-item>
				<el-form-item label="启用状态" prop="is_enable" required>
					<el-radio-group v-model="formData.is_enable">
						<el-radio :label="1">启用</el-radio>
						<el-radio :label="0">停用</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
		</popup>
	</div>
</template>
<script lang="ts" setup>
	import type { FormInstance } from 'element-plus'
	import { toolAdd, toolEdit } from '@/api/baseconfig/toolLists';
	import Popup from '@/components/popup/index.vue'
	import { getModulesKey } from '@/router'

	const emit = defineEmits(['success', 'close'])
	const formRef = shallowRef<FormInstance>()
	const popupRef = shallowRef<InstanceType<typeof Popup>>()
	const mode = ref('add')
	const popupTitle = computed(() => {
		return mode.value == 'edit' ? '编辑工具' : '新增工具'
	})

	const componentsOptions = ref(getModulesKey())
	const querySearch = (queryString : string, cb : any) => {
		const results = queryString
			? componentsOptions.value.filter((item) =>
				item.toLowerCase().includes(queryString.toLowerCase())
			)
			: componentsOptions.value
		cb(results.map((item) => ({ value: item })))
	}
	const formData = reactive({
		id: '',
		//名称
		name: '',
		//icon
		icon: '',
		//类型
		type: 1,
		//链接
		link: '',
		//appid
		app_id: '',
		//启用状态
		is_enable: 1
	})

	const formRules = {
		name: [
			{
				required: true,
				message: '请输入名称',
				trigger: 'blur'
			}
		],
		icon: [
			{
				required: true,
				message: '请上传icon',
				trigger: 'blur'
			}
		],
		type: [
			{
				required: true,
				message: '请选择类型',
				trigger: 'blur'
			}
		],
		link: [
			{
				required: true,
				message: '请输入链接',
				trigger: 'blur'
			}
		],
		app_id: [
			{
				required: true,
				message: '请输入appid',
				trigger: 'blur'
			}
		]
	}
	const menuOptions = ref<any[]>([])

	const handleSubmit = async () => {
		await formRef.value?.validate()
		let newFormData = JSON.parse(JSON.stringify(formData));
		mode.value == 'edit' ? await toolEdit(newFormData) : await toolAdd(newFormData)
		popupRef.value?.close()
		emit('success')
	}

	const open = (type = 'add') => {
		mode.value = type
		popupRef.value?.open()
	}
	
	const setFormData = (data : Record<any, any>) => {
		for (const key in formData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				formData[key] = data[key]
			}
		}
	}

	const getDetail = async (row : Record<string, any>) => {
		setFormData(row)
	}

	const handleClose = () => {
		emit('close')
	}

	defineExpose({
		open,
		setFormData,
		getDetail
	})
</script>

<style scoped lang="scss">
// 	.dataTable {
// 		width: 80%;
// 		line-height: 40px;
// 		text-align: left;
// 		border-top: 1px solid #eaeaea;
// 		border-left: 1px solid #eaeaea;
// 		border-right: 1px solid #eaeaea;

// 		tr {
// 			// display: flex;

// 			th {
// 				flex: 1;
// 				height: 40px;
// 				padding: 0 16px;
// 				font-weight: 500;
// 				font-size: 12px;
// 				color: #999999;
// 				// background: #f9f8f8;
// 				border-bottom: 1px solid #eaeaea;
// 				border-right: 1px solid #eaeaea;
// 			}

// 			td {
// 				flex: 1;
// 				height: 40px;
// 				padding: 0 16px;
// 				font-size: 12px;
// 				color: #666;
// 				border-bottom: 1px solid #eaeaea;
// 				border-right: 1px solid #eaeaea;
// 			}
// 		}
// 	}
</style>